package com.leemon.mall.sys;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;


import java.io.File;


/**
 * @author limenglong
 * @create 2019-09-03 16:43
 * @desc mybatis plus 自动生成代码工具类
 *  运行此工具后生成的代码可按需修改，但是修改过代码后请不要再次使用次工具生成代码
 *  否则新生成的代码会覆盖掉已经修改过的代码
 **/
public class Generator {

    public static void main(String[] args) {
        AutoGenerator mpg = new AutoGenerator();

        //设置模板引擎
//        mpg.setTemplateEngine(new FreemarkerTemplateEngine());

        //全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setAuthor("leemon");
        globalConfig.setActiveRecord(true);
        globalConfig.setOutputDir("/Users/limenglong/IdeaProjects/spring-mall/mall-sys/src/main/java");
        globalConfig.setBaseResultMap(true);
        globalConfig.setFileOverride(true);
        globalConfig.setEnableCache(false);

        //自定义文件命名
        globalConfig.setControllerName("%sController");
        globalConfig.setMapperName("%sMapper");
        globalConfig.setServiceName("%sService");
        globalConfig.setServiceImplName("%sServiceImpl");
        globalConfig.setXmlName("%sMapper");


        mpg.setGlobalConfig(globalConfig);


        //数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl("jdbc:mysql://127.0.0.1:3306/my_shops?allowMultiQueries=true&useSSL=false&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true");
        dataSourceConfig.setDbType(DbType.MYSQL);
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        dataSourceConfig.setUsername("root");
        dataSourceConfig.setPassword("li0512/*");
        mpg.setDataSource(dataSourceConfig);

        //策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        //修改表前缀
        strategyConfig.setTablePrefix("tz_");
        //数据库表映射到实体的命名策略
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);

        strategyConfig.setInclude("tz_sys_user_role",
                "tz_sys_user",
                "tz_sys_role_menu",
                "tz_sys_role",
                "tz_sys_menu",
                "tz_sys_config",
                "tz_sys_log");
        mpg.setStrategy(strategyConfig);

        //包配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent("com.leemon.mall.sys");
        packageConfig.setMapper("dao");
        packageConfig.setController("controller");
        packageConfig.setEntity("model");
        packageConfig.setService("service");
        packageConfig.setServiceImpl("service.impl");
        packageConfig.setXml("mapper");
        mpg.setPackageInfo(packageConfig);

        mpg.execute();

        File source = new File("/Users/limenglong/IdeaProjects/spring-mall/mall-sys/src/main/java/com/leemon/mall/sys/mapper");
        File target = new File("/Users/limenglong/IdeaProjects/spring-mall/mall-sys/src/main/resources/mapper");
        if (source.exists() && source.isDirectory()) {
            if(target.exists()&&target.isDirectory()){
                FileUtil.del(target);
            }
            FileUtil.move(source, target, true);
        }
        System.out.println("complete!");
    }
}
